
#include "stdafx.h"
#include "RF_LogSys.h"

//////////////////////////////////////////////////////////////////////////

MyLock::MyLock(void)
{
	InitializeCriticalSection(&m_cs);
}

MyLock::~MyLock(void)
{
	DeleteCriticalSection(&m_cs);
}

void MyLock::On()
{
	EnterCriticalSection(&m_cs);
}

void MyLock::Off()
{
	LeaveCriticalSection(&m_cs);
}



//////////////////////////////////////////////////////////////////////////
LogDateDir::LogDateDir(){

	memset( strDateDirName, 0 , DATETIMEBUFFER_MAX );
}

LogDateDir::LogDateDir(char *pStrDateDirName ){

	UINT dirNameLen = 0;
	if( pStrDateDirName != NULL ){
	
		dirNameLen = strlen( pStrDateDirName );
	}

	if( dirNameLen < DATETIMEBUFFER_MAX ){

		strcpy( strDateDirName,pStrDateDirName );
	}
}

LogDateDir::~LogDateDir(){

}

void LogDateDir::SetLogDateDir( char *pStrDateDirName ){

	UINT dirNameLen = 0;
	if( pStrDateDirName != NULL ){

		dirNameLen = strlen( pStrDateDirName );
	}

	if( dirNameLen < DATETIMEBUFFER_MAX ){

		strcpy( strDateDirName,pStrDateDirName );
	}
}

LogWriteBuffer::LogWriteBuffer(){

}

LogWriteBuffer::LogWriteBuffer(char *pStrFileName,
							   char *pStrFileTitle,
							   char *pStrDateDir,
							   char *pStrWriteContent){

	//UINT strLen = 0;
	
   if( pStrFileName	    == NULL ||
	   pStrFileTitle	== NULL ||
	   strDateDir       == NULL ||
	   pStrWriteContent == NULL ){

		return;
   }



   if(  strlen( pStrFileName)	  >= NAME_MAX   ||
	    strlen( pStrFileTitle)    >= NAME_MAX   ||
		strlen( strDateDir )      >= NAME_MAX   ||
		strlen( pStrWriteContent) >= NAME_MAX  ){
		
		return;
   }

   strcpy( strFileName,pStrFileName );
   strcpy( strFileTitle,pStrFileTitle);
   strcpy( strDateDir, pStrDateDir );
   strcpy( strWriteBuffer,pStrWriteContent);

}

LogWriteBuffer::~LogWriteBuffer(){

}

int LogWriteBuffer::Set_LogWriteBufferData(char *pStrFileName, 
										   char *pStrFileTitle, 
										   char *pStrDateDir, 
										   char *pStrWriteContent){
	 if( pStrFileName	    == NULL ||
	   pStrFileTitle	== NULL ||
	   strDateDir       == NULL ||
	   pStrWriteContent == NULL ){

		return FALSE;
   }



   if(  strlen( pStrFileName)	  >= NAME_MAX   ||
	    strlen( pStrFileTitle)    >= NAME_MAX   ||
		strlen( strDateDir )      >= NAME_MAX   ||
		strlen( pStrWriteContent) >= NAME_MAX  ){
		
		return FALSE;
   }

   strcpy( strFileName,pStrFileName );
   strcpy( strFileTitle,pStrFileTitle);
   strcpy( strDateDir, pStrDateDir );
   strcpy( strWriteBuffer,pStrWriteContent);

   return TRUE;
}

//////////////////////////////////////////////////////////////////////////
RF_LogSys::RF_LogSys(){


}

RF_LogSys::~RF_LogSys(){


}

bool RF_LogSys::LogSys_FromateLocalTimeToString( LPTSTR strTime ){

	SYSTEMTIME st;
   
	char year[4],month[2],date[2],hour[2],minute[2],sec[2],millsec[3];
	if( strTime == NULL  ){

		return false;
	}
	
	GetLocalTime( &st);

	sprintf( year,"%d",st.wYear);

	if( st.wMonth < 10 ){

		sprintf(month,"0%d" ,st.wMonth);
	}
	else{
		sprintf( month, "%d",st.wMonth);
	}

	if( st.wDay < 10 ){
		sprintf( date,"0%d",st.wDay);
	}
	else{
		sprintf(date,"%d",st.wDay);
	}

	if( st.wHour < 10 ){

		sprintf( hour,"0%d",st.wHour);
	}
	else{
		sprintf( hour,"%d",st.wHour );
	}

	if( st.wMinute < 10 ){
		
		sprintf( minute,"0%d",st.wMinute );
	}
	else{
		sprintf( minute,"%d",st.wMinute );
	}

	if( st.wSecond < 10 ){

		sprintf( sec,"0%d",st.wSecond);
	}
	else{
		sprintf(sec,"%d",st.wSecond);
	}

	if( st.wMilliseconds < 10 ){

		sprintf(millsec,"00%d",st.wMilliseconds );
	}
	else if(   st.wMilliseconds < 100 ){

		sprintf( millsec,"0%d",st.wMilliseconds);
	}
	else{
		sprintf( millsec,"%d",st.wMilliseconds );
	}

	sprintf( strTime,"%s-%s-%s %s:%s:%s:%s",year,month,date,hour,minute,sec,millsec );

	return true;
	
}

UINT RF_LogSys::LogSys_WorkerThreadProc( LPVOID pParam ){

	RF_LogSys* pThis = reinterpret_cast<RF_LogSys*>(pParam);
	
	return TRUE;
}

bool RF_LogSys::LogSys_GetLocalTimeToFileNameString( LPTSTR  strTime){

	SYSTEMTIME st;

	char year[4],month[2],date[2];
	if( strTime == NULL  ){

		return false;
	}

	GetLocalTime( &st);

	sprintf( year,"%d",st.wYear);

	if( st.wMonth < 10 ){

		sprintf(month,"0%d" ,st.wMonth);
	}
	else{
		sprintf( month, "%d",st.wMonth);
	}

	if( st.wDay < 10 ){
		sprintf( date,"0%d",st.wDay);
	}
	else{
		sprintf(date,"%d",st.wDay);
	}

	sprintf( strTime,"%s_%s_%s",year,month,date );

	return true;
}